package com.whfc.wxmp.api.base.controller;

import com.whfc.common.base.BaseController;
import com.whfc.common.enums.DeviceType;
import com.whfc.common.result.Result;
import com.whfc.common.result.ResultUtil;
import com.whfc.common.util.JSONUtil;
import com.whfc.wxmp.annotation.CurrentWxUser;
import com.whfc.emp.dto.DeviceBindEmp;
import com.whfc.entity.dto.WxDeviceBindCheckDTO;
import com.whfc.entity.param.WxBindCheckParam;
import com.whfc.fuum.entity.WxUser;
import com.whfc.wxmp.api.emp.service.WxEmpService;
import com.whfc.wxmp.api.mach.v1.service.WxmpMachService;
import com.whfc.wxmp.util.RequestConetxtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 小程序中的设备管理
 *
 * @author guodong_sun
 * @Description:
 * @date 2019年8月5日
 */
@Deprecated
@RestController
@RequestMapping("/mp/api/device")
public class WxDeviceController extends BaseController {

    @Autowired
    private WxEmpService wxEmpService;

    @Autowired
    private WxmpMachService wxMachService;

    @PostMapping("/bind/emp")
    public Result bindEmp(@CurrentWxUser WxUser user, @Validated @RequestBody DeviceBindEmp request) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                user.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "硬件绑定人员", JSONUtil.toString(request));
        wxEmpService.bindEmp(user, request);
        return ResultUtil.success();
    }

    /**
     * 绑定校验
     *
     * @param user
     * @param request
     * @return
     */
    @PostMapping("/bindCheck")
    public Result bindCheck(@CurrentWxUser WxUser user, @RequestBody @Validated WxBindCheckParam request) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                user.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "硬件绑定前检验", RequestConetxtUtil.getParameters());
        DeviceType deviceType = DeviceType.parseByValue(request.getType());
        String sn = request.getSn();
        WxDeviceBindCheckDTO result;
        switch (deviceType) {
            case mach:
                result = wxMachService.bindCheck(sn);
                break;
            case helmet:
                result = wxEmpService.bindCheck(sn);
                break;
            default:
                result = new WxDeviceBindCheckDTO();
                result.setAvailable(0);
                result.setMsg("硬件类型错误");
                break;
        }
        return ResultUtil.success(result);
    }

}
